package com.hd.trans.files.domain;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;

import com.hd.trans.files.bean.Audio;
import com.hd.trans.files.model.FileSortModel;
import com.hd.trans.files.repository.AudioDataRepository;
import com.hd.trans.utils.SharedFileUtils;

import java.util.List;

import io.reactivex.rxjava3.disposables.Disposable;

/**
 * Author: yangtianwen
 * Time: 2021/12/15
 * Des: 音频数据源业务类
 */
public class RequestAudioDataUseCase extends BaseUseCase {

    // Audio 的数据源
    private final AudioDataRepository mAudioDataRepository;

    private MutableLiveData<List<Audio>> mAudioDataObserver;

    private Disposable mLoadAudioDisposable;

    public RequestAudioDataUseCase() {
        mAudioDataRepository = new AudioDataRepository();
    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        if (mLoadAudioDisposable != null) {
            mLoadAudioDisposable.dispose();
        }
    }

    /**
     * 加载音频数据
     */
    public void loadAudioData(Context context, String folder, Callback<List<Audio>> callback) {
        FileSortModel.ModeItem sortModeItem = SharedFileUtils.getInstance(context)
                .getFileSortModeItem();
        mLoadAudioDisposable = mAudioDataRepository.requestAllAudio(context, folder, AUDIO_SUFFIXS, sortModeItem)
                .subscribe(audioData -> {
                    if (callback != null) callback.onValueChange(audioData);
                    mAudioDataObserver.setValue(audioData);
                });
    }

    /**
     * 加载文档数据
     */
    public void loadDocData(Context context, String folder, Callback<List<Audio>> callback) {
        FileSortModel.ModeItem sortModeItem = SharedFileUtils.getInstance(context)
                .getFileSortModeItem();
        mLoadAudioDisposable = mAudioDataRepository.requestAllAudio(context, folder, DOC_SUFFIXS, sortModeItem)
                .subscribe(audioData -> {
                    if (callback != null) callback.onValueChange(audioData);
                    mAudioDataObserver.setValue(audioData);
                });
    }

    /**
     * 加载录音数据
     */
    public void loadSoundRecordingData(Context context, Callback<List<Audio>> callback) {
        FileSortModel.ModeItem sortModeItem = SharedFileUtils.getInstance(context)
                .getFileSortModeItem();
        mLoadAudioDisposable = mAudioDataRepository.requestSoundRecordingData(context, sortModeItem)
                .subscribe((audio, throwable) -> {
                    if (callback != null) callback.onValueChange(audio);
                    mAudioDataObserver.setValue(audio);
                });
    }

    /**
     * 加载剪辑数据
     */
    public void loadClipData(Context context, Callback<List<Audio>> callback) {
        FileSortModel.ModeItem sortModeItem = SharedFileUtils.getInstance(context)
                .getFileSortModeItem();
        mLoadAudioDisposable = mAudioDataRepository.requestEditAudioData(context, sortModeItem)
                .subscribe((audio, throwable) -> {
                    if (callback != null) callback.onValueChange(audio);
                    mAudioDataObserver.setValue(audio);
                });
    }

    public RequestAudioDataUseCase addLifecycleOwner(LifecycleOwner owner) {
        owner.getLifecycle().addObserver(this);
        return this;
    }

    public RequestAudioDataUseCase addAudioDataObserver(MutableLiveData<List<Audio>> audioDataObserver) {
        this.mAudioDataObserver = audioDataObserver;
        return this;
    }
}
